const path = require("path");
const db = require(path.join(__dirname,'./../middleware/mysql/db'));
var dataresult = require(path.join(__dirname,'./../module/dataresult'));
const func = require(path.join(__dirname,'./../module/func'));

/**
 * 查询功能列表 
 * */
async function queryFunc(params) {
    return new Promise((resolve, reject) => {
        try{
            let whereparam = new Array();
            if(params.plugid!="" || typeof(params.plugid)!="undefined"){
                whereparam.push({key:"plugid",value:params.plugid});
            }
            if(params.funcid!="" || typeof(params.funcid)!="undefined"){
                whereparam.push({key:"id",value:params.funcid});
            }
            let sql = db.getSelect(params,func.colum,func.tablename,whereparam);
            db.query(sql, function (err, rows) {
                dataresult.succSearch(rows);
                err ? reject(err) : resolve(dataresult)
            });
        }catch(err){
            dataresult.errorExec("funcservice 程序执行异常");
            resolve(dataresult);
            console.log(err);
        }
    })
}

/**
 * 保存功能-目前未使用20210106
 * @param {*} params 
 */
function saveFunc(params) {
    return new Promise((resolve, reject) => {
        try{
            console.log(params);
            let sql = db.getInsert(params,func.colum,func.tablename);
            db.query(sql, function (err, rows) {
                dataresult.succExec(rows);
                err ? reject(err) : resolve(dataresult)
        });
        }catch(err){
            dataresult.errorExec("funcservice 程序执行异常");
            resolve(dataresult);
            console.log(err);
        }
    })
}

/**
 * 修改功能-目前未使用20210106
 * @param {*} params 
 */
function updateFunc(params) {
    return new Promise((resolve, reject) => {
        try{
            console.log(params);
            if(params.id=="" && typeof(params.id) != "undefined"){
                dataresult.errorExec("修改的数据id为空，请检查");
                return dataresult;
            }
            let sql = db.getUpdate(params,func.colum,func.tablename);
            db.query(sql, function (err, rows) {
                dataresult.succExec(rows);
                err ? reject(err) : resolve(dataresult)
            });
        }catch(err){
            dataresult.errorExec("funcservice 程序执行异常");
            resolve(dataresult);
            console.log(err);
        }
    })
}

/**
 * 删除功能-目前未使用20210106
 * @param {*} params 
 */
function deleteFunc(params) {
    return new Promise((resolve, reject) => {
        try{
            console.log(params);
            if(params.id=="" && typeof(params.id) != "undefined"){
                dataresult.errorExec("删除的数据id为空，请检查");
                return dataresult;
            }
            let sql = db.getDelete(params,func.colum,func.tablename);
            db.query(sql, function (err, rows) {
                dataresult.succExec(rows);
                err ? reject(err) : resolve(dataresult)
            });
        }catch(err){
            dataresult.errorExec("funcservice 程序执行异常");
            resolve(dataresult);
            console.log(err);
        }
    })
}

exports.queryFunc = queryFunc;
exports.saveFunc = saveFunc;
exports.updateFunc = updateFunc;
exports.deleteFunc = deleteFunc;